<template>
  <el-dialog v-model="dialogVisible" title="会员详情" width="80%" top="50px" class="custom-dialog">
    <div class="details-container">
      <!-- 基本信息 -->
      <el-card class="info-card">
        <div class="card-header">
          <span>基本信息</span>
        </div>
        <div class="info-content">
          <div class="info-item">
            <span>用户昵称：</span>
            <span>{{ userInfo.name }}</span>
          </div>
          <div class="info-item">
            <span>手机号：</span>
            <span>{{ userInfo.phone }}</span>
          </div>
          <div class="info-item">
            <span>标签：</span>
            <span>
              <el-tag v-for="tag in userInfo.labels" :key="tag.buyerLabelId" style="margin-right: 5px">
                {{ tag.labelName }}
              </el-tag>
            </span>
          </div>
          <div class="info-item">
            <span>累计消费金额：</span>
            <span>{{ userInfo.totalConsumAmount }}</span>
          </div>
          <div class="info-item">
            <span>购买次数：</span>
            <span>{{ userInfo.totalConsumTimes }}</span>
          </div>
          <div class="info-item">
            <span>最近消费时间：</span>
            <span>{{ userInfo.lastConsumTime }}</span>
          </div>
        </div>
      </el-card>

      <!-- 订单信息 -->
      <el-card class="order-card">
        <div class="card-header">
          <span>订单信息</span>
        </div>
        <div class="tab_list">
          <p :class="{ active: activeTab === 1 }" @click="switchTab(1)">全部订单</p>
          <p :class="{ active: activeTab === 2 }" @click="switchTab(2)">待付款</p>
          <p :class="{ active: activeTab === 3 }" @click="switchTab(3)">待发货</p>
          <p :class="{ active: activeTab === 4 }" @click="switchTab(4)">待收货</p>
          <p :class="{ active: activeTab === 5 }" @click="switchTab(5)">已完成</p>
        </div>
        <el-table :data="orderList" style="width: 100%">
          <el-table-column prop="orderNo" label="订单号" />
          <el-table-column prop="productName" label="商品名称" />
          <el-table-column prop="price" label="价格" />
          <el-table-column prop="quantity" label="数量" />
          <el-table-column prop="totalAmount" label="总金额" />
          <el-table-column prop="orderStatus" label="订单状态" />
          <el-table-column prop="createTime" label="创建时间" />
        </el-table>
      </el-card>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { getMemberDetails } from '@/api/memberApi'

const dialogVisible = ref(false)
const activeTab = ref(1)

// 用户信息
const userInfo = ref<{
  name: string
  phone: string
  labels: Array<{
    buyerLabelId: string
    labelName: string
  }>
  totalConsumAmount: number
  totalConsumTimes: number
  lastConsumTime: string
}>({
  name: '',
  phone: '',
  labels: [],
  totalConsumAmount: 0,
  totalConsumTimes: 0,
  lastConsumTime: ''
})

// 订单列表
const orderList = ref<any[]>([])

// 切换标签页
const switchTab = (tab: number) => {
  activeTab.value = tab
  // 这里可以添加获取不同状态订单的逻辑
}

// 获取用户详情
const getUser = (params: { buyerUserId: string }) => {
  dialogVisible.value = true
  getMemberDetails(params).then((res: any) => {
    if (res.code === '200') {
      userInfo.value = res.data.userInfo || {}
      orderList.value = res.data.orderList || []
    }
  })
}

defineExpose({
  getUser
})
</script>

<style lang="scss" scoped>
.custom-dialog {
  .details-container {
    max-height: 70vh;
    overflow-y: auto;

    .info-card,
    .order-card {
      margin-bottom: 20px;

      .card-header {
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 15px;
        padding-bottom: 10px;
        border-bottom: 1px solid #ebeef5;
      }

      .info-content {
        .info-item {
          display: flex;
          margin-bottom: 15px;
          font-size: 14px;

          span:first-child {
            width: 120px;
            color: #999;
          }

          span:last-child {
            flex: 1;
            color: #333;
          }
        }
      }
    }

    .tab_list {
      overflow: hidden;
      margin-bottom: 20px;

      p {
        float: left;
        padding: 0 30px;
        font-size: 14px;
        border: 1px #e0e5eb solid;
        box-sizing: border-box;
        color: #999999;
        height: 36px;
        line-height: 36px;
        cursor: pointer;
        margin-right: 10px;

        &:nth-child(-n + 2) {
          border-right: 0;
        }

        &:first-child {
          border-radius: 4px 0 0 4px;
        }

        &:last-child {
          border-radius: 0 4px 4px 0;
        }

        &:hover {
          color: #3a68f2;
        }
      }

      .active {
        background-color: #3a68f2;
        color: #fff;
        border: 1px solid #3a68f2;
      }
    }
  }
}
</style>